﻿using System;
using System.Collections;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Windows.Forms;
using RaGE.Properties;

namespace RaGE
{
    public partial class MainWindow
    {
        private void PWNPCInitilaize()
        {
            clientOffsets = new ArrayList();

            if (File.Exists(Application.StartupPath + "\\pwoffsets.txt"))
            {
                try
                {
                    var sr = new StreamReader(Application.StartupPath + "\\pwoffsets.txt", Encoding.Unicode);

                    var seperator = new[] {' '};
                    string line;

                    while (!string.IsNullOrEmpty((line = sr.ReadLine())))
                    {
                        if (!line.StartsWith("Name=")) continue;

                        comboBox_AssistantClient.Items.Add(line.Substring(5));

                        var offset = new PWOffset();

                        // target base offset chain
                        var readLine = sr.ReadLine();

                        if (readLine != null)
                        {
                            var values = readLine.Split(seperator);
                            offset.BaseChain = new int[values.Length];

                            for (var i = 0; i < values.Length; i++)
                                offset.BaseChain[i] = Int32.Parse(values[i], NumberStyles.HexNumber);
                        }

                        offset.DirX = Int32.Parse(sr.ReadLine(), NumberStyles.HexNumber);
                        offset.DirY = Int32.Parse(sr.ReadLine(), NumberStyles.HexNumber);
                        offset.DirZ = Int32.Parse(sr.ReadLine(), NumberStyles.HexNumber);
                        offset.PosX = Int32.Parse(sr.ReadLine(), NumberStyles.HexNumber);
                        offset.PosY = Int32.Parse(sr.ReadLine(), NumberStyles.HexNumber);
                        offset.PosZ = Int32.Parse(sr.ReadLine(), NumberStyles.HexNumber);

                        clientOffsets.Add(offset);
                    }

                    sr.Close();

                    if (comboBox_AssistantClient.Items.Count > 0)
                        comboBox_AssistantClient.SelectedIndex = 0;
                }
// ReSharper disable EmptyGeneralCatchClause
                catch (Exception)
// ReSharper restore EmptyGeneralCatchClause
                {
                }
            }

            PWnpcDB = new SortedList();

            if (!File.Exists(Application.StartupPath + "\\pwnpcdb.txt")) return;

            try
            {
                var sr = new StreamReader(Application.StartupPath + "\\pwnpcdb.txt", Encoding.Unicode);

                var seperator = new[] {'\t'};
                string line;

                while (!string.IsNullOrEmpty((line = sr.ReadLine())))
                {
                    if (!line.Contains("\t")) continue;

                    var split = line.Split(seperator);
                    PWnpcDB.Add(split[0], split[1]);
                }

                sr.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(Resources.ErrLoadNPCDB + e.Message);
            }
        }

        private string PWNPCName(int id)
        {
            var result = (string)PWnpcDB[id.ToString(CultureInfo.InvariantCulture)];
            return result ?? ("Not found...");
        }

        private void PWLoadCreatures(BinaryReader br)
        {
            dataGridView_PWCreatures.Rows.Clear();

            pwnpcgen.creature_sets = new PWCreatureSets[pwnpcgen.creature_sets_count];

            for (var i = 0; i < pwnpcgen.creature_sets.Length; i++)
            {
                pwnpcgen.creature_sets[i] = new PWCreatureSets();
                pwnpcgen.creature_sets[i].spawn_mode = br.ReadInt32();
                pwnpcgen.creature_sets[i].creature_groups_count = br.ReadInt32();
                pwnpcgen.creature_sets[i].spawn_x = br.ReadSingle();
                pwnpcgen.creature_sets[i].spawn_alt = br.ReadSingle();
                pwnpcgen.creature_sets[i].spawn_z = br.ReadSingle();
                pwnpcgen.creature_sets[i].rot_1 = br.ReadSingle();
                pwnpcgen.creature_sets[i].rot_2 = br.ReadSingle();
                pwnpcgen.creature_sets[i].rot_3 = br.ReadSingle();
                pwnpcgen.creature_sets[i].spread_x = br.ReadSingle();
                pwnpcgen.creature_sets[i].spread_alt = br.ReadSingle();
                pwnpcgen.creature_sets[i].spread_z = br.ReadSingle();
                pwnpcgen.creature_sets[i].unknown_7 = br.ReadInt32();
                pwnpcgen.creature_sets[i].unknown_8 = br.ReadInt32();
                pwnpcgen.creature_sets[i].unknown_9 = br.ReadBoolean();
                pwnpcgen.creature_sets[i].unknown_10 = br.ReadBoolean();
                pwnpcgen.creature_sets[i].unknown_11 = br.ReadBoolean();
                pwnpcgen.creature_sets[i].unknown_12 = br.ReadInt32();

                if (pwnpcgen.version == 10)
                {
                    pwnpcgen.creature_sets[i].trigger = br.ReadInt32();
                    pwnpcgen.creature_sets[i].unknown_14 = br.ReadInt32();
                    pwnpcgen.creature_sets[i].unknown_15 = br.ReadInt32();
                }
                else
                {
                    pwnpcgen.creature_sets[i].trigger = 0;
                    pwnpcgen.creature_sets[i].unknown_14 = 0;
                    pwnpcgen.creature_sets[i].unknown_15 = 0;
                }

                pwnpcgen.creature_sets[i].creature_groups = new PWCreatureGroup[pwnpcgen.creature_sets[i].creature_groups_count];

                for (var j = 0; j < pwnpcgen.creature_sets[i].creature_groups.Length; j++)
                {
                    pwnpcgen.creature_sets[i].creature_groups[j] = new PWCreatureGroup();

                    pwnpcgen.creature_sets[i].creature_groups[j].id = br.ReadInt32();
                    pwnpcgen.creature_sets[i].creature_groups[j].amount = br.ReadInt32();
                    pwnpcgen.creature_sets[i].creature_groups[j].respawn = br.ReadInt32();
                    pwnpcgen.creature_sets[i].creature_groups[j].unknown_2 = br.ReadInt32();
                    pwnpcgen.creature_sets[i].creature_groups[j].unknown_3 = br.ReadInt32();
                    pwnpcgen.creature_sets[i].creature_groups[j].unknown_4 = br.ReadInt32();
                    pwnpcgen.creature_sets[i].creature_groups[j].unknown_5 = br.ReadInt32();
                    pwnpcgen.creature_sets[i].creature_groups[j].unknown_6 = br.ReadInt32();
                    pwnpcgen.creature_sets[i].creature_groups[j].unknown_7 = br.ReadInt32();
                    pwnpcgen.creature_sets[i].creature_groups[j].unknown_8 = br.ReadInt32();
                    pwnpcgen.creature_sets[i].creature_groups[j].unknown_9 = br.ReadBoolean();
                    pwnpcgen.creature_sets[i].creature_groups[j].unknown_10 = br.ReadBoolean();
                    pwnpcgen.creature_sets[i].creature_groups[j].unknown_11 = br.ReadBoolean();
                    pwnpcgen.creature_sets[i].creature_groups[j].unknown_12 = br.ReadBoolean();
                    pwnpcgen.creature_sets[i].creature_groups[j].unknown_13 = br.ReadInt32();
                    pwnpcgen.creature_sets[i].creature_groups[j].unknown_14 = br.ReadInt32();
                    pwnpcgen.creature_sets[i].creature_groups[j].unknown_15 = br.ReadInt32();
                    pwnpcgen.creature_sets[i].creature_groups[j].unknown_16 = br.ReadInt32();
                }

                dataGridView_PWCreatures.Rows.Add(new object[]
                {
                    pwnpcgen.creature_sets[i].creature_groups_count.ToString(CultureInfo.InvariantCulture),
                    Column1.Items[pwnpcgen.creature_sets[i].spawn_mode].ToString(),
                    pwnpcgen.creature_sets[i].spawn_x.ToString("F3"),
                    pwnpcgen.creature_sets[i].spawn_alt.ToString("F3"),
                    pwnpcgen.creature_sets[i].spawn_z.ToString("F3"),
                    pwnpcgen.creature_sets[i].rot_1.ToString("F3"),
                    pwnpcgen.creature_sets[i].rot_2.ToString("F3"),
                    pwnpcgen.creature_sets[i].rot_3.ToString("F3"),
                    pwnpcgen.creature_sets[i].spread_x.ToString("F3"),
                    pwnpcgen.creature_sets[i].spread_alt.ToString("F3"),
                    pwnpcgen.creature_sets[i].spread_z.ToString("F3"),
                    Column12.Items[pwnpcgen.creature_sets[i].unknown_7].ToString(),
                    pwnpcgen.creature_sets[i].unknown_8.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[i].unknown_9.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[i].unknown_10.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[i].unknown_11.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[i].unknown_12.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[i].trigger.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[i].unknown_14.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[i].unknown_15.ToString(CultureInfo.InvariantCulture)
                });

                if (pwnpcgen.creature_sets[i].creature_groups_count > 0)
                {
                    dataGridView_PWCreatures.Rows[i].HeaderCell.Value =
                        "[" + pwnpcgen.creature_sets[i].creature_groups[0].id.ToString(CultureInfo.InvariantCulture) + "] " +
                        PWNPCName(pwnpcgen.creature_sets[i].creature_groups[0].id);
                }
            }
        }

        private void PWLoadResources(BinaryReader br)
        {
            dataGridView_PWResources.Rows.Clear();
            pwnpcgen.resource_sets = new PWResourceSets[pwnpcgen.resource_sets_count];

            for (var i = 0; i < pwnpcgen.resource_sets.Length; i++)
            {
                pwnpcgen.resource_sets[i] = new PWResourceSets();
                pwnpcgen.resource_sets[i].spawn_x = br.ReadSingle();
                pwnpcgen.resource_sets[i].spawn_alt = br.ReadSingle();
                pwnpcgen.resource_sets[i].spawn_z = br.ReadSingle();
                pwnpcgen.resource_sets[i].spread_x = br.ReadSingle();
                pwnpcgen.resource_sets[i].spread_z = br.ReadSingle();
                pwnpcgen.resource_sets[i].resource_groups_count = br.ReadInt32();
                pwnpcgen.resource_sets[i].unknown_1 = br.ReadBoolean();
                pwnpcgen.resource_sets[i].unknown_2 = br.ReadBoolean();
                pwnpcgen.resource_sets[i].unknown_3 = br.ReadBoolean();
                pwnpcgen.resource_sets[i].unknown_4 = br.ReadInt32();

                if (pwnpcgen.version == 10)
                {
                    pwnpcgen.resource_sets[i].unknown_5a = br.ReadByte();
                    pwnpcgen.resource_sets[i].unknown_5b = br.ReadByte();
                    pwnpcgen.resource_sets[i].unknown_5c = br.ReadByte();
                    pwnpcgen.resource_sets[i].unknown_trigger = br.ReadInt32();
                    pwnpcgen.resource_sets[i].unknown_6 = br.ReadByte();
                    pwnpcgen.resource_sets[i].unknown_7 = br.ReadBoolean();
                    pwnpcgen.resource_sets[i].unknown_8 = br.ReadBoolean();
                    pwnpcgen.resource_sets[i].unknown_9 = br.ReadBoolean();
                }
                else
                {
                    pwnpcgen.resource_sets[i].unknown_5a = 192;
                    pwnpcgen.resource_sets[i].unknown_5b = 63;
                    pwnpcgen.resource_sets[i].unknown_5c = 0;
                    pwnpcgen.resource_sets[i].unknown_trigger = 0;
                    pwnpcgen.resource_sets[i].unknown_6 = 0;
                    pwnpcgen.resource_sets[i].unknown_7 = false;
                    pwnpcgen.resource_sets[i].unknown_8 = false;
                    pwnpcgen.resource_sets[i].unknown_9 = false;
                }

                pwnpcgen.resource_sets[i].resource_groups = new PWResourceGroup[pwnpcgen.resource_sets[i].resource_groups_count];

                for (var j = 0; j < pwnpcgen.resource_sets[i].resource_groups.Length; j++)
                {
                    pwnpcgen.resource_sets[i].resource_groups[j] = new PWResourceGroup();
                    pwnpcgen.resource_sets[i].resource_groups[j].unknown_1 = br.ReadInt32();
                    pwnpcgen.resource_sets[i].resource_groups[j].id = br.ReadInt32();
                    pwnpcgen.resource_sets[i].resource_groups[j].respawn = br.ReadInt32();
                    pwnpcgen.resource_sets[i].resource_groups[j].amount = br.ReadInt32();
                    pwnpcgen.resource_sets[i].resource_groups[j].unknown_2 = br.ReadInt32();
                }

                dataGridView_PWResources.Rows.Add(new object[]
                {
                    pwnpcgen.resource_sets[i].resource_groups_count.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.resource_sets[i].spawn_x.ToString("F3"),
                    pwnpcgen.resource_sets[i].spawn_alt.ToString("F3"),
                    pwnpcgen.resource_sets[i].spawn_z.ToString("F3"),
                    pwnpcgen.resource_sets[i].spread_x.ToString("F3"),
                    pwnpcgen.resource_sets[i].spread_z.ToString("F3"),
                    pwnpcgen.resource_sets[i].unknown_1.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.resource_sets[i].unknown_2.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.resource_sets[i].unknown_3.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.resource_sets[i].unknown_4.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.resource_sets[i].unknown_5a.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.resource_sets[i].unknown_5b.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.resource_sets[i].unknown_5c.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.resource_sets[i].unknown_trigger.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.resource_sets[i].unknown_6.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.resource_sets[i].unknown_7.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.resource_sets[i].unknown_8.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.resource_sets[i].unknown_9.ToString(CultureInfo.InvariantCulture)
                });

                if (pwnpcgen.resource_sets[i].resource_groups_count > 0)
                    dataGridView_PWResources.Rows[i].HeaderCell.Value =
                        pwnpcgen.resource_sets[i].resource_groups[0].id.ToString(CultureInfo.InvariantCulture);
            }
        }

        private void PWLoadDynamics(BinaryReader br)
        {
            dataGridView_PWDynamics.Rows.Clear();
            pwnpcgen.dynamics = new PWDynamic[pwnpcgen.dynamics_count];

            for (var i = 0; i < pwnpcgen.dynamics.Length; i++)
            {
                pwnpcgen.dynamics[i] = new PWDynamic();
                pwnpcgen.dynamics[i].id = br.ReadInt32();
                pwnpcgen.dynamics[i].spawn_x = br.ReadSingle();
                pwnpcgen.dynamics[i].spawn_alt = br.ReadSingle();
                pwnpcgen.dynamics[i].spawn_z = br.ReadSingle();
                pwnpcgen.dynamics[i].unknown_5 = br.ReadByte();
                pwnpcgen.dynamics[i].unknown_6 = br.ReadByte();
                pwnpcgen.dynamics[i].unknown_7 = br.ReadByte();
                pwnpcgen.dynamics[i].trigger = br.ReadInt32();
                pwnpcgen.dynamics[i].unknown_8 = br.ReadByte();

                dataGridView_PWDynamics.Rows.Add(new object[]
                {
                    pwnpcgen.dynamics[i].id.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.dynamics[i].spawn_x.ToString("F3"),
                    pwnpcgen.dynamics[i].spawn_alt.ToString("F3"),
                    pwnpcgen.dynamics[i].spawn_z.ToString("F3"),
                    pwnpcgen.dynamics[i].unknown_5.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.dynamics[i].unknown_6.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.dynamics[i].unknown_7.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.dynamics[i].trigger.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.dynamics[i].unknown_8.ToString(CultureInfo.InvariantCulture)
                });

                dataGridView_PWDynamics.Rows[i].HeaderCell.Value =
                    (dataGridView_PWDynamics.Rows.Count - 1).ToString(CultureInfo.InvariantCulture);
            }
        }

        private void PWLoadTriggers(BinaryReader br)
        {
            dataGridView_PWTriggers.Rows.Clear();
            pwnpcgen.triggers = new PWTrigger[pwnpcgen.triggers_count];

            for (var i = 0; i < pwnpcgen.triggers.Length; i++)
            {
                pwnpcgen.triggers[i] = new PWTrigger();
                pwnpcgen.triggers[i].unknown_1 = br.ReadInt32();
                pwnpcgen.triggers[i].unknown_2 = br.ReadInt32();
                pwnpcgen.triggers[i].name = br.ReadBytes(128);
                pwnpcgen.triggers[i].unknown_3 = br.ReadBoolean();
                pwnpcgen.triggers[i].unknown_4 = br.ReadInt32();
                pwnpcgen.triggers[i].unknown_5 = br.ReadInt32();
                pwnpcgen.triggers[i].unknown_6 = br.ReadBoolean();
                pwnpcgen.triggers[i].unknown_7 = br.ReadBoolean();
                pwnpcgen.triggers[i].year_1 = br.ReadInt32();
                pwnpcgen.triggers[i].month_1 = br.ReadInt32();
                pwnpcgen.triggers[i].week_day_1 = br.ReadInt32();
                pwnpcgen.triggers[i].day_1 = br.ReadInt32();
                pwnpcgen.triggers[i].hour_1 = br.ReadInt32();
                pwnpcgen.triggers[i].minute_1 = br.ReadInt32();
                pwnpcgen.triggers[i].year_2 = br.ReadInt32();
                pwnpcgen.triggers[i].month_2 = br.ReadInt32();
                pwnpcgen.triggers[i].week_day_2 = br.ReadInt32();
                pwnpcgen.triggers[i].day_2 = br.ReadInt32();
                pwnpcgen.triggers[i].hour_2 = br.ReadInt32();
                pwnpcgen.triggers[i].minute_2 = br.ReadInt32();
                pwnpcgen.triggers[i].duration = br.ReadInt32();

                var enc = Encoding.GetEncoding("GBK");

                dataGridView_PWTriggers.Rows.Add(new object[]
                {
                    pwnpcgen.triggers[i].unknown_1.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.triggers[i].unknown_2.ToString(CultureInfo.InvariantCulture),
                    enc.GetString(pwnpcgen.triggers[i].name),
                    pwnpcgen.triggers[i].unknown_3.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.triggers[i].unknown_4.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.triggers[i].unknown_5.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.triggers[i].unknown_6.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.triggers[i].unknown_7.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.triggers[i].year_1.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.triggers[i].month_1.ToString(CultureInfo.InvariantCulture),
                    Column42.Items[pwnpcgen.triggers[i].week_day_1 + 1].ToString(),
                    pwnpcgen.triggers[i].day_1.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.triggers[i].hour_1.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.triggers[i].minute_1.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.triggers[i].year_2.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.triggers[i].month_2.ToString(CultureInfo.InvariantCulture),
                    Column43.Items[pwnpcgen.triggers[i].week_day_2 + 1].ToString(),
                    pwnpcgen.triggers[i].day_2.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.triggers[i].hour_2.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.triggers[i].minute_2.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.triggers[i].duration.ToString(CultureInfo.InvariantCulture)
                });
                
                dataGridView_PWTriggers.Rows[i].HeaderCell.Value =
                    (dataGridView_PWTriggers.Rows.Count - 1).ToString(CultureInfo.InvariantCulture);
            }
        }

        private void PWSaveCreatures(BinaryWriter bw)
        {
            foreach (var t in pwnpcgen.creature_sets)
            {
                bw.Write(t.spawn_mode);
                bw.Write(t.creature_groups_count);
                bw.Write(t.spawn_x);
                bw.Write(t.spawn_alt);
                bw.Write(t.spawn_z);
                bw.Write(t.rot_1);
                bw.Write(t.rot_2);
                bw.Write(t.rot_3);
                bw.Write(t.spread_x);
                bw.Write(t.spread_alt);
                bw.Write(t.spread_z);
                bw.Write(t.unknown_7);
                bw.Write(t.unknown_8);
                bw.Write(t.unknown_9);
                bw.Write(t.unknown_10);
                bw.Write(t.unknown_11);
                bw.Write(t.unknown_12);
                bw.Write(t.trigger);
                bw.Write(t.unknown_14);
                bw.Write(t.unknown_15);

                foreach (var u in t.creature_groups)
                {
                    bw.Write(u.id);
                    bw.Write(u.amount);
                    bw.Write(u.respawn);
                    bw.Write(u.unknown_2);
                    bw.Write(u.unknown_3);
                    bw.Write(u.unknown_4);
                    bw.Write(u.unknown_5);
                    bw.Write(u.unknown_6);
                    bw.Write(u.unknown_7);
                    bw.Write(u.unknown_8);
                    bw.Write(u.unknown_9);
                    bw.Write(u.unknown_10);
                    bw.Write(u.unknown_11);
                    bw.Write(u.unknown_12);
                    bw.Write(u.unknown_13);
                    bw.Write(u.unknown_14);
                    bw.Write(u.unknown_15);
                    bw.Write(u.unknown_16);
                }
            }
        }

        private void PWSaveResources(BinaryWriter bw)
        {
            foreach (var t in pwnpcgen.resource_sets)
            {
                bw.Write(t.spawn_x);
                bw.Write(t.spawn_alt);
                bw.Write(t.spawn_z);
                bw.Write(t.spread_x);
                bw.Write(t.spread_z);
                bw.Write(t.resource_groups_count);
                bw.Write(t.unknown_1);
                bw.Write(t.unknown_2);
                bw.Write(t.unknown_3);
                bw.Write(t.unknown_4);
                bw.Write(t.unknown_5a);
                bw.Write(t.unknown_5b);
                bw.Write(t.unknown_5c);
                bw.Write(t.unknown_trigger);
                bw.Write(t.unknown_6);
                bw.Write(t.unknown_7);
                bw.Write(t.unknown_8);
                bw.Write(t.unknown_9);

                foreach (var u in t.resource_groups)
                {
                    bw.Write(u.unknown_1);
                    bw.Write(u.id);
                    bw.Write(u.respawn);
                    bw.Write(u.amount);
                    bw.Write(u.unknown_2);
                }
            }
        }

        private void PWSaveDynamics(BinaryWriter bw)
        {
            foreach (var t in pwnpcgen.dynamics)
            {
                bw.Write(t.id);
                bw.Write(t.spawn_x);
                bw.Write(t.spawn_alt);
                bw.Write(t.spawn_z);
                bw.Write(t.unknown_5);
                bw.Write(t.unknown_6);
                bw.Write(t.unknown_7);
                bw.Write(t.trigger);
                bw.Write(t.unknown_8);
            }
        }

        private void PWSaveTriggers(BinaryWriter bw)
        {
            foreach (var t in pwnpcgen.triggers)
            {
                bw.Write(t.unknown_1);
                bw.Write(t.unknown_2);
                bw.Write(t.name);
                bw.Write(t.unknown_3);
                bw.Write(t.unknown_4);
                bw.Write(t.unknown_5);
                bw.Write(t.unknown_6);
                bw.Write(t.unknown_7);
                bw.Write(t.year_1);
                bw.Write(t.month_1);
                bw.Write(t.week_day_1);
                bw.Write(t.day_1);
                bw.Write(t.hour_1);
                bw.Write(t.minute_1);
                bw.Write(t.year_2);
                bw.Write(t.month_2);
                bw.Write(t.week_day_2);
                bw.Write(t.day_2);
                bw.Write(t.hour_2);
                bw.Write(t.minute_2);
                bw.Write(t.duration);
            }
        }

        private void PWSearchClick(object sender, EventArgs e)
        {
            if (pwnpcgen == null) return;

            try
            {
                var pattern = Convert.ToInt32(textBox_search.Text);

                if (tabControl_PWNPCGen.SelectedIndex == 0)
                {
                    if (dataGridView_PWCreatures.CurrentRow != null)
                    {
                        var i = dataGridView_PWCreatures.CurrentRow.Index;
                        if (i < 0) i = 0;

                        for ( ; i < pwnpcgen.creature_sets_count; i++)
                        {
                            var n = 0;
                            // first run -> drop selected creaturegroup item
                            if (i == dataGridView_PWCreatures.CurrentRow.Index)
                            {
                                if (dataGridView_PWCreatureGroups.CurrentRow != null)
                                    n = dataGridView_PWCreatureGroups.CurrentRow.Index + 1;
                            }

                            if (n >= dataGridView_PWCreatureGroups.Rows.Count)
                            {
                                i++;
                                n = 0;
                            }

                            if (i >= pwnpcgen.creature_sets_count) continue;

                            for ( ; n < pwnpcgen.creature_sets[i].creature_groups_count; n++)
                            {
                                if (pwnpcgen.creature_sets[i].creature_groups[n].id != pattern) continue;

                                dataGridView_PWCreatures.CurrentCell = dataGridView_PWCreatures.Rows[i].Cells[0];
                                dataGridView_PWCreatureGroups.CurrentCell = dataGridView_PWCreatureGroups.Rows[n].Cells[0];
                                return;
                            }
                        }
                        MessageBox.Show(Resources.SearchEndFile);
                    }
                }

                if (tabControl_JDNPCGen.SelectedIndex == 1)
                {
                    if (dataGridView_PWResources.CurrentRow != null)
                    {
                        var i = dataGridView_PWResources.CurrentRow.Index;
                        if (i < 0) i = 0;

                        for ( ; i < pwnpcgen.resource_sets_count; i++)
                        {
                            var n = 0;
                            // first run -> drop selected creaturegroup item
                            if (i == dataGridView_PWResources.CurrentRow.Index)
                            {
                                if (dataGridView_PWResourceGroups.CurrentRow != null)
                                    n = dataGridView_PWResourceGroups.CurrentRow.Index + 1;
                            }

                            if (n >= dataGridView_PWResourceGroups.Rows.Count)
                            {
                                i++;
                                n = 0;
                            }

                            if (i >= pwnpcgen.resource_sets_count) continue;

                            for ( ; n < pwnpcgen.resource_sets[i].resource_groups_count; n++)
                            {
                                if (pwnpcgen.resource_sets[i].resource_groups[n].id != pattern) continue;

                                dataGridView_PWResources.CurrentCell = dataGridView_PWResources.Rows[i].Cells[0];
                                dataGridView_PWResourceGroups.CurrentCell = dataGridView_PWResourceGroups.Rows[n].Cells[0];
                                return;
                            }
                        }
                    }
                    MessageBox.Show(Resources.SearchNoResults);
                }

                if (tabControl_JDNPCGen.SelectedIndex == 2)
                {
                    if (dataGridView_PWDynamics.CurrentRow != null)
                    {
                        var i = dataGridView_PWDynamics.CurrentRow.Index + 1;

                        for ( ; i < pwnpcgen.dynamics_count; i++)
                        {
                            if (pwnpcgen.dynamics[i].id != pattern) continue;

                            dataGridView_PWDynamics.CurrentCell = dataGridView_PWDynamics.Rows[i].Cells[0];
                            return;
                        }
                    }
                    MessageBox.Show(Resources.SearchNoResults);
                }

                if (tabControl_JDNPCGen.SelectedIndex != 3) return;

                if (dataGridView_PWTriggers.CurrentRow != null)
                {
                    var i = dataGridView_PWTriggers.CurrentRow.Index + 1;

                    for ( ; i < pwnpcgen.triggers_count; i++)
                    {
                        if (pwnpcgen.triggers[i].unknown_1 != pattern) continue;

                        dataGridView_PWTriggers.CurrentCell = dataGridView_PWTriggers.Rows[i].Cells[0];
                        return;
                    }
                }
                MessageBox.Show(Resources.SearchNoResults);
            }
// ReSharper disable EmptyGeneralCatchClause
            catch
// ReSharper restore EmptyGeneralCatchClause
            {
                //MessageBox.Show(Resources.NPCFormatErr);
            }
        }

        private void PWAssistantGetValuesClick(object sender, EventArgs e)
        {
            if (comboBox_AssistantClient.SelectedIndex <= -1) return;

            var procList = Process.GetProcessesByName("elementclient");

            if (procList.Length <= 0) return;

            var hProcess = procList[0].Handle;
            var lpBuffer = new Byte[4];
            var offset = (PWOffset)clientOffsets[comboBox_AssistantClient.SelectedIndex];
            var baseOffset = 0;
                    
            foreach (var t in offset.BaseChain)
            {
                ReadProcessMemory(hProcess, (IntPtr) (baseOffset + t), lpBuffer, 4, null);
                baseOffset = BitConverter.ToInt32(lpBuffer, 0);
            }
                    
            ReadProcessMemory(hProcess, (IntPtr) (baseOffset + offset.DirX), lpBuffer, 4, null);
            var dirX = BitConverter.ToSingle(lpBuffer, 0);
                    
            ReadProcessMemory(hProcess, (IntPtr) (baseOffset + offset.DirY), lpBuffer, 4, null);
            var dirY = BitConverter.ToSingle(lpBuffer, 0);
                    
            ReadProcessMemory(hProcess, (IntPtr) (baseOffset + offset.DirZ), lpBuffer, 4, null);
            var dirZ = BitConverter.ToSingle(lpBuffer, 0);
                    
            ReadProcessMemory(hProcess, (IntPtr) (baseOffset + offset.PosX), lpBuffer, 4, null);
            var posX = BitConverter.ToSingle(lpBuffer, 0);
                    
            ReadProcessMemory(hProcess, (IntPtr) (baseOffset + offset.PosY), lpBuffer, 4, null);
            var posY = BitConverter.ToSingle(lpBuffer, 0);
                    
            ReadProcessMemory(hProcess, (IntPtr) (baseOffset + offset.PosZ), lpBuffer, 4, null);
            var posZ = BitConverter.ToSingle(lpBuffer, 0);
                    
            // Apply values
            int r;

            if (tabControl_NPCJDPW.SelectedTab == tabPage_PWCreatures && dataGridView_PWCreatures.CurrentCell != null)
            {
                r = dataGridView_PWCreatures.CurrentCell.RowIndex;

                /*if (dataGridView_creatureGroups.CurrentCell)
                        {
                            int n = dataGridView_creatureGroups.CurrentCell.RowIndex;
                            dataGridView_creatureGroups.Rows[n].Cells[0].Value = textBox_AssistantNPCid.Text;
                            npcgen.creature_sets[r].creature_groups[n].id = System.Convert.ToInt32(textBox_AssistantNPCid.Text);
                            
                            if(n == 0)
                            {
                                dataGridView_creatures.Rows[r].HeaderCell.Value =
                                    "[" + textBox_AssistantNPCid.Text + "] " + npcName(npcgen.creature_sets[r].creature_groups[0].id);
                            }
                        }*/

                dataGridView_PWCreatures.Rows[r].Cells[2].Value = posX.ToString("F3");
                pwnpcgen.creature_sets[r].spawn_x = posX;
                        
                dataGridView_PWCreatures.Rows[r].Cells[3].Value = posY.ToString("F3");
                pwnpcgen.creature_sets[r].spawn_alt = posY;
                        
                dataGridView_PWCreatures.Rows[r].Cells[4].Value = posZ.ToString("F3");
                pwnpcgen.creature_sets[r].spawn_z = posZ;
                        
                dataGridView_PWCreatures.Rows[r].Cells[5].Value = dirX.ToString("F3");
                pwnpcgen.creature_sets[r].rot_1 = dirX;
                        
                dataGridView_PWCreatures.Rows[r].Cells[6].Value = dirY.ToString("F3");
                pwnpcgen.creature_sets[r].rot_2 = dirY;
                        
                dataGridView_PWCreatures.Rows[r].Cells[7].Value = dirZ.ToString("F3");
                pwnpcgen.creature_sets[r].rot_3 = dirZ;
            }

            if (tabControl_NPCJDPW.SelectedTab == tabPage_PWResources && dataGridView_PWResources.CurrentCell != null)
            {
                r = dataGridView_PWResources.CurrentCell.RowIndex;
                        
                dataGridView_PWResources.Rows[r].Cells[1].Value = posX.ToString("F3");
                pwnpcgen.resource_sets[r].spawn_x = posX;
                        
                dataGridView_PWResources.Rows[r].Cells[2].Value = posY.ToString("F3");
                pwnpcgen.resource_sets[r].spawn_alt = posY;
                        
                dataGridView_PWResources.Rows[r].Cells[3].Value = posZ.ToString("F3");
                pwnpcgen.resource_sets[r].spawn_z = posZ;
            }

            if (tabControl_NPCJDPW.SelectedTab != tabPage_PWDynamics) return;

            r = dataGridView_PWDynamics.CurrentCell.RowIndex;
                        
            dataGridView_PWDynamics.Rows[r].Cells[1].Value = posX.ToString("F3");
            pwnpcgen.dynamics[r].spawn_x = posX;

            dataGridView_PWDynamics.Rows[r].Cells[2].Value = posY.ToString("F3");
            pwnpcgen.dynamics[r].spawn_alt = posY;
                        
            dataGridView_PWDynamics.Rows[r].Cells[3].Value = posZ.ToString("F3");
            pwnpcgen.dynamics[r].spawn_z = posZ;
        }

        private void PWChangeCreatures(object sender, DataGridViewCellEventArgs e)
        {
            if (pwnpcgen == null || dataGridView_PWCreatures.CurrentCell == null) return;

            try
            {
                var r = dataGridView_PWCreatures.CurrentCell.RowIndex;

                switch (dataGridView_PWCreatures.CurrentCell.ColumnIndex)
                {
                    case 1:
                        pwnpcgen.creature_sets[r].spawn_mode = Column1.Items.IndexOf(dataGridView_PWCreatures.CurrentCell.Value);
                        break;

                    case 2:
                        pwnpcgen.creature_sets[r].spawn_x = Convert.ToSingle(dataGridView_PWCreatures.CurrentCell.Value);
                        break;

                    case 3:
                        pwnpcgen.creature_sets[r].spawn_alt = Convert.ToSingle(dataGridView_PWCreatures.CurrentCell.Value);
                        break;

                    case 4:
                        pwnpcgen.creature_sets[r].spawn_z = Convert.ToSingle(dataGridView_PWCreatures.CurrentCell.Value);
                        break;

                    case 5:
                        pwnpcgen.creature_sets[r].rot_1 = Convert.ToSingle(dataGridView_PWCreatures.CurrentCell.Value);
                        break;

                    case 6:
                        pwnpcgen.creature_sets[r].rot_2 = Convert.ToSingle(dataGridView_PWCreatures.CurrentCell.Value);
                        break;

                    case 7:
                        pwnpcgen.creature_sets[r].rot_3 = Convert.ToSingle(dataGridView_PWCreatures.CurrentCell.Value);
                        break;

                    case 8:
                        pwnpcgen.creature_sets[r].spread_x = Convert.ToSingle(dataGridView_PWCreatures.CurrentCell.Value);
                        break;

                    case 9:
                        pwnpcgen.creature_sets[r].spread_alt = Convert.ToSingle(dataGridView_PWCreatures.CurrentCell.Value);
                        break;

                    case 10:
                        pwnpcgen.creature_sets[r].spread_z = Convert.ToSingle(dataGridView_PWCreatures.CurrentCell.Value);
                        break;

                    case 11:
                        pwnpcgen.creature_sets[r].unknown_7 = Column12.Items.IndexOf(dataGridView_PWCreatures.CurrentCell.Value);
                        break;

                    case 12:
                        pwnpcgen.creature_sets[r].unknown_8 = Convert.ToInt32(dataGridView_PWCreatures.CurrentCell.Value);
                        break;

                    case 13:
                        pwnpcgen.creature_sets[r].unknown_9 = Convert.ToBoolean(dataGridView_PWCreatures.CurrentCell.Value);
                        break;

                    case 14:
                        pwnpcgen.creature_sets[r].unknown_10 = Convert.ToBoolean(dataGridView_PWCreatures.CurrentCell.Value);
                        break;

                    case 15:
                        pwnpcgen.creature_sets[r].unknown_11 = Convert.ToBoolean(dataGridView_PWCreatures.CurrentCell.Value);
                        break;

                    case 16:
                        pwnpcgen.creature_sets[r].unknown_12 = Convert.ToInt32(dataGridView_PWCreatures.CurrentCell.Value);
                        break;

                    case 17:
                        pwnpcgen.creature_sets[r].trigger = Convert.ToInt32(dataGridView_PWCreatures.CurrentCell.Value);
                        break;

                    case 18:
                        pwnpcgen.creature_sets[r].unknown_14 = Convert.ToInt32(dataGridView_PWCreatures.CurrentCell.Value);
                        break;

                    case 19:
                        pwnpcgen.creature_sets[r].unknown_15 = Convert.ToInt32(dataGridView_PWCreatures.CurrentCell.Value);
                        break;
                }
            }
            catch
            {
                MessageBox.Show(Resources.NPCFormatErr);
            }
        }

        private void PWChangeCreatureGroups(object sender, DataGridViewCellEventArgs e)
        {
            if (pwnpcgen == null || dataGridView_PWCreatures.CurrentCell == null ||
                dataGridView_PWCreatureGroups.CurrentCell == null) return;

            try
            {
                var r = dataGridView_PWCreatures.CurrentCell.RowIndex;
                var n = dataGridView_PWCreatureGroups.CurrentCell.RowIndex;

                switch (dataGridView_PWCreatureGroups.CurrentCell.ColumnIndex)
                {
                    case 0:
                        pwnpcgen.creature_sets[r].creature_groups[n].id = Convert.ToInt32(dataGridView_PWCreatureGroups.CurrentCell.Value);
                        if (n == 0)
                        {
                            dataGridView_PWCreatures.Rows[r].HeaderCell.Value =
                                "[" + pwnpcgen.creature_sets[r].creature_groups[0].id.ToString(CultureInfo.InvariantCulture) +
                                "] " + PWNPCName(pwnpcgen.creature_sets[r].creature_groups[0].id);
                        }
                        dataGridView_PWCreatureGroups.Rows[n].HeaderCell.Value = PWNPCName(pwnpcgen.creature_sets[r].creature_groups[n].id);
                        break;
                        
                    case 1:
                        pwnpcgen.creature_sets[r].creature_groups[n].amount = Convert.ToInt32(dataGridView_PWCreatureGroups.CurrentCell.Value);
                        break;
                        
                    case 2:
                        pwnpcgen.creature_sets[r].creature_groups[n].respawn = Convert.ToInt32(dataGridView_PWCreatureGroups.CurrentCell.Value);
                        break;
                        
                    case 3:
                        pwnpcgen.creature_sets[r].creature_groups[n].unknown_2 = Convert.ToInt32(dataGridView_PWCreatureGroups.CurrentCell.Value);
                        break;
                        
                    case 4:
                        pwnpcgen.creature_sets[r].creature_groups[n].unknown_3 = Convert.ToInt32(dataGridView_PWCreatureGroups.CurrentCell.Value);
                        break;
                        
                    case 5:
                        pwnpcgen.creature_sets[r].creature_groups[n].unknown_4 = Convert.ToInt32(dataGridView_PWCreatureGroups.CurrentCell.Value);
                        break;
                        
                    case 6:
                        pwnpcgen.creature_sets[r].creature_groups[n].unknown_5 = Convert.ToInt32(dataGridView_PWCreatureGroups.CurrentCell.Value);
                        break;
                        
                    case 7:
                        pwnpcgen.creature_sets[r].creature_groups[n].unknown_6 = Convert.ToInt32(dataGridView_PWCreatureGroups.CurrentCell.Value);
                        break;
                        
                    case 8:
                        pwnpcgen.creature_sets[r].creature_groups[n].unknown_7 = Convert.ToInt32(dataGridView_PWCreatureGroups.CurrentCell.Value);
                        break;
                        
                    case 9:
                        pwnpcgen.creature_sets[r].creature_groups[n].unknown_8 = Convert.ToInt32(dataGridView_PWCreatureGroups.CurrentCell.Value);
                        break;
                        
                    case 10:
                        pwnpcgen.creature_sets[r].creature_groups[n].unknown_9 = Convert.ToBoolean(dataGridView_PWCreatureGroups.CurrentCell.Value);
                        break;
                       
                    case 11:
                        pwnpcgen.creature_sets[r].creature_groups[n].unknown_10 = Convert.ToBoolean(dataGridView_PWCreatureGroups.CurrentCell.Value);
                        break;
                        
                    case 12:
                        pwnpcgen.creature_sets[r].creature_groups[n].unknown_11 = Convert.ToBoolean(dataGridView_PWCreatureGroups.CurrentCell.Value);
                        break;
                        
                    case 13:
                        pwnpcgen.creature_sets[r].creature_groups[n].unknown_12 = Convert.ToBoolean(dataGridView_PWCreatureGroups.CurrentCell.Value);
                        break;
                        
                    case 14:
                        pwnpcgen.creature_sets[r].creature_groups[n].unknown_13 = Convert.ToInt32(dataGridView_PWCreatureGroups.CurrentCell.Value);
                        break;
                        
                    case 15:
                        pwnpcgen.creature_sets[r].creature_groups[n].unknown_14 = Convert.ToInt32(dataGridView_PWCreatureGroups.CurrentCell.Value);
                        break;
                        
                    case 16:
                        pwnpcgen.creature_sets[r].creature_groups[n].unknown_15 = Convert.ToInt32(dataGridView_PWCreatureGroups.CurrentCell.Value);
                        break;
                        
                    case 17:
                        pwnpcgen.creature_sets[r].creature_groups[n].unknown_16 = Convert.ToInt32(dataGridView_PWCreatureGroups.CurrentCell.Value);
                        break;
                }
            }
            catch
            {
                MessageBox.Show(Resources.NPCFormatErr);
            }
        }

        private void PWClickRowCreature(object sender, DataGridViewCellEventArgs e)
        {
            dataGridView_PWCreatureGroups.Rows.Clear();
            dataGridView_PWCreatureGroups.ContextMenuStrip = null;

            if (pwnpcgen == null || e.RowIndex <= -1 || e.RowIndex >= pwnpcgen.creature_sets.Length) return;

            for (var i = 0; i < pwnpcgen.creature_sets[e.RowIndex].creature_groups_count; i++)
            {
                dataGridView_PWCreatureGroups.Rows.Add(new object[]
                {
                    pwnpcgen.creature_sets[e.RowIndex].creature_groups[i].id.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[e.RowIndex].creature_groups[i].amount.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[e.RowIndex].creature_groups[i].respawn.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_2.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_3.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_4.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_5.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_6.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_7.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_8.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_9.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_10.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_11.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_12.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_13.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_14.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_15.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.creature_sets[e.RowIndex].creature_groups[i].unknown_16.ToString(CultureInfo.InvariantCulture)
                });

                dataGridView_PWCreatureGroups.CellValueChanged -= PWChangeCreatureGroups;
                dataGridView_PWCreatureGroups.Rows[i].HeaderCell.Value = PWNPCName(pwnpcgen.creature_sets[e.RowIndex].creature_groups[i].id);
                dataGridView_PWCreatureGroups.CellValueChanged += PWChangeCreatureGroups;
            }

            dataGridView_PWCreatureGroups.ContextMenuStrip = contextPWDataGrid;
        }

        private void PWChangeResources(object sender, DataGridViewCellEventArgs e)
        {
            if (pwnpcgen == null || dataGridView_PWResources.CurrentCell == null) return;

            try
            {
                var r = dataGridView_PWResources.CurrentCell.RowIndex;

                switch (dataGridView_PWResources.CurrentCell.ColumnIndex)
                {
                    case 1:
                        pwnpcgen.resource_sets[r].spawn_x = Convert.ToSingle(dataGridView_PWResources.CurrentCell.Value);
                        break;
                    
                    case 2:
                        pwnpcgen.resource_sets[r].spawn_alt = Convert.ToSingle(dataGridView_PWResources.CurrentCell.Value);
                        break;
                    
                    case 3:
                        pwnpcgen.resource_sets[r].spawn_z = Convert.ToSingle(dataGridView_PWResources.CurrentCell.Value);
                        break;
                    
                    case 4:
                        pwnpcgen.resource_sets[r].spread_x = Convert.ToSingle(dataGridView_PWResources.CurrentCell.Value);
                        break;
                    
                    case 5:
                        pwnpcgen.resource_sets[r].spread_z = Convert.ToSingle(dataGridView_PWResources.CurrentCell.Value);
                        break;
                    
                    case 6:
                        pwnpcgen.resource_sets[r].unknown_1 = Convert.ToBoolean(dataGridView_PWResources.CurrentCell.Value);
                        break;
                    
                    case 7:
                        pwnpcgen.resource_sets[r].unknown_2 = Convert.ToBoolean(dataGridView_PWResources.CurrentCell.Value);
                        break;
                    
                    case 8:
                        pwnpcgen.resource_sets[r].unknown_3 = Convert.ToBoolean(dataGridView_PWResources.CurrentCell.Value);
                        break;
                    
                    case 9:
                        pwnpcgen.resource_sets[r].unknown_4 = Convert.ToInt32(dataGridView_PWResources.CurrentCell.Value);
                        break;
                    
                    case 10:
                        pwnpcgen.resource_sets[r].unknown_5a = Convert.ToByte(dataGridView_PWResources.CurrentCell.Value);
                        break;
                    
                    case 11:
                        pwnpcgen.resource_sets[r].unknown_5b = Convert.ToByte(dataGridView_PWResources.CurrentCell.Value);
                        break;
                    
                    case 12:
                        pwnpcgen.resource_sets[r].unknown_5c = Convert.ToByte(dataGridView_PWResources.CurrentCell.Value);
                        break;
                    
                    case 13:
                        pwnpcgen.resource_sets[r].unknown_trigger = Convert.ToInt32(dataGridView_PWResources.CurrentCell.Value);
                        break;
                    
                    case 14:
                        pwnpcgen.resource_sets[r].unknown_6 = Convert.ToByte(dataGridView_PWResources.CurrentCell.Value);
                        break;
                    
                    case 15:
                        pwnpcgen.resource_sets[r].unknown_7 = Convert.ToBoolean(dataGridView_PWResources.CurrentCell.Value);
                        break;
                    
                    case 16:
                        pwnpcgen.resource_sets[r].unknown_8 = Convert.ToBoolean(dataGridView_PWResources.CurrentCell.Value);
                        break;
                    
                    case 17:
                        pwnpcgen.resource_sets[r].unknown_9 = Convert.ToBoolean(dataGridView_PWResources.CurrentCell.Value);
                        break;
                }
            }
            catch
            {
                MessageBox.Show(Resources.NPCFormatErr);
            }
        }

        private void PWClickRowResource(object sender, DataGridViewCellEventArgs e)
        {
            dataGridView_PWResourceGroups.Rows.Clear();
            dataGridView_PWResourceGroups.ContextMenuStrip = null;

            if (pwnpcgen == null || e.RowIndex <= -1 || e.RowIndex >= pwnpcgen.resource_sets.Length) return;

            for (var i = 0; i < pwnpcgen.resource_sets[e.RowIndex].resource_groups_count; i++)
            {
                dataGridView_PWResourceGroups.Rows.Add(new object[]
                {
                    pwnpcgen.resource_sets[e.RowIndex].resource_groups[i].unknown_1.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.resource_sets[e.RowIndex].resource_groups[i].id.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.resource_sets[e.RowIndex].resource_groups[i].respawn.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.resource_sets[e.RowIndex].resource_groups[i].amount.ToString(CultureInfo.InvariantCulture),
                    pwnpcgen.resource_sets[e.RowIndex].resource_groups[i].unknown_2.ToString(CultureInfo.InvariantCulture)
                });
            }

            dataGridView_PWResourceGroups.ContextMenuStrip = contextPWDataGrid;
        }

        private void PWChangeResourceGroups(object sender, DataGridViewCellEventArgs e)
        {
            if (pwnpcgen == null || dataGridView_PWResources.CurrentCell == null ||
                dataGridView_PWResourceGroups.CurrentCell == null) return;

            try
            {
                var r = dataGridView_PWResources.CurrentCell.RowIndex;
                var n = dataGridView_PWResourceGroups.CurrentCell.RowIndex;

                switch (dataGridView_PWResourceGroups.CurrentCell.ColumnIndex)
                {
                    case 0:
                        pwnpcgen.resource_sets[r].resource_groups[n].unknown_1 = Convert.ToInt32(dataGridView_PWResourceGroups.CurrentCell.Value);
                        break;
                    
                    case 1:
                        pwnpcgen.resource_sets[r].resource_groups[n].id = Convert.ToInt32(dataGridView_PWResourceGroups.CurrentCell.Value);
                        if (n == 0)
                            dataGridView_PWResources.Rows[r].HeaderCell.Value =
                                pwnpcgen.resource_sets[r].resource_groups[0].id.ToString(CultureInfo.InvariantCulture);
                        break;
                    
                    case 2:
                        pwnpcgen.resource_sets[r].resource_groups[n].respawn = Convert.ToInt32(dataGridView_PWResourceGroups.CurrentCell.Value);
                        break;
                    
                    case 3:
                        pwnpcgen.resource_sets[r].resource_groups[n].amount = Convert.ToInt32(dataGridView_PWResourceGroups.CurrentCell.Value);
                        break;
                    
                    case 4:
                        pwnpcgen.resource_sets[r].resource_groups[n].unknown_2 = Convert.ToInt32(dataGridView_PWResourceGroups.CurrentCell.Value);
                        break;
                }
            }
            catch
            {
                MessageBox.Show(Resources.NPCFormatErr);
            }
        }

        private void PWChangeDynamics(object sender, DataGridViewCellEventArgs e)
        {
            if (pwnpcgen == null || dataGridView_PWDynamics.CurrentCell == null) return;

            try
            {
                var r = dataGridView_PWDynamics.CurrentCell.RowIndex;

                switch (dataGridView_PWDynamics.CurrentCell.ColumnIndex)
                {
                    case 0:
                        pwnpcgen.dynamics[r].id = Convert.ToInt32(dataGridView_PWDynamics.CurrentCell.Value);
                        break;
                        
                    case 1:
                        pwnpcgen.dynamics[r].spawn_x = Convert.ToSingle(dataGridView_PWDynamics.CurrentCell.Value);
                        break;
                        
                    case 2:
                        pwnpcgen.dynamics[r].spawn_alt = Convert.ToSingle(dataGridView_PWDynamics.CurrentCell.Value);
                        break;
                        
                    case 3:
                        pwnpcgen.dynamics[r].spawn_z = Convert.ToSingle(dataGridView_PWDynamics.CurrentCell.Value);
                        break;
                        
                    case 4:
                        pwnpcgen.dynamics[r].unknown_5 = Convert.ToByte(dataGridView_PWDynamics.CurrentCell.Value);
                        break;
                        
                    case 5:
                        pwnpcgen.dynamics[r].unknown_6 = Convert.ToByte(dataGridView_PWDynamics.CurrentCell.Value);
                        break;
                        
                    case 6:
                        pwnpcgen.dynamics[r].unknown_7 = Convert.ToByte(dataGridView_PWDynamics.CurrentCell.Value);
                        break;
                        
                    case 7:
                        pwnpcgen.dynamics[r].trigger = Convert.ToInt32(dataGridView_PWDynamics.CurrentCell.Value);
                        break;
                        
                    case 8:
                        pwnpcgen.dynamics[r].unknown_8 = Convert.ToByte(dataGridView_PWDynamics.CurrentCell.Value);
                        break;
                }
            }
            catch
            {
                MessageBox.Show(Resources.NPCFormatErr);
            }
        }

        private void PWChangeTriggers(object sender, DataGridViewCellEventArgs e)
        {
            if (pwnpcgen == null || dataGridView_PWTriggers.CurrentCell == null) return;

            try
            {
                var r = dataGridView_PWTriggers.CurrentCell.RowIndex;

                switch (dataGridView_PWTriggers.CurrentCell.ColumnIndex)
                {
                    case 0:
                        pwnpcgen.triggers[r].unknown_1 = Convert.ToInt32(dataGridView_PWTriggers.CurrentCell.Value);
                        break;
                    
                    case 1:
                        pwnpcgen.triggers[r].unknown_2 = Convert.ToInt32(dataGridView_PWTriggers.CurrentCell.Value);
                        break;
                    
                    case 2:
                        pwnpcgen.triggers[r].name = Common.GetBytesGBK(dataGridView_PWTriggers.CurrentCell.Value.ToString());
                        break;
                    
                    case 3:
                        pwnpcgen.triggers[r].unknown_3 = Convert.ToBoolean(dataGridView_PWTriggers.CurrentCell.Value);
                        break;
                    
                    case 4:
                        pwnpcgen.triggers[r].unknown_4 = Convert.ToInt32(dataGridView_PWTriggers.CurrentCell.Value);
                        break;
                    
                    case 5:
                        pwnpcgen.triggers[r].unknown_5 = Convert.ToInt32(dataGridView_PWTriggers.CurrentCell.Value);
                        break;
                    
                    case 6:
                        pwnpcgen.triggers[r].unknown_6 = Convert.ToBoolean(dataGridView_PWTriggers.CurrentCell.Value);
                        break;
                    
                    case 7:
                        pwnpcgen.triggers[r].unknown_7 = Convert.ToBoolean(dataGridView_PWTriggers.CurrentCell.Value);
                        break;
                    
                    case 8:
                        pwnpcgen.triggers[r].year_1 = Convert.ToInt32(dataGridView_PWTriggers.CurrentCell.Value);
                        break;
                    
                    case 9:
                        pwnpcgen.triggers[r].month_1 = Convert.ToInt32(dataGridView_PWTriggers.CurrentCell.Value);
                        break;
                    
                    case 10:
                        pwnpcgen.triggers[r].week_day_1 = Column42.Items.IndexOf(dataGridView_PWTriggers.CurrentCell.Value) - 1;
                        break;
                    
                    case 11:
                        pwnpcgen.triggers[r].day_1 = Convert.ToInt32(dataGridView_PWTriggers.CurrentCell.Value);
                        break;
                    
                    case 12:
                        pwnpcgen.triggers[r].hour_1 = Convert.ToInt32(dataGridView_PWTriggers.CurrentCell.Value);
                        break;
                    
                    case 13:
                        pwnpcgen.triggers[r].minute_1 = Convert.ToInt32(dataGridView_PWTriggers.CurrentCell.Value);
                        break;
                    
                    case 14:
                        pwnpcgen.triggers[r].year_2 = Convert.ToInt32(dataGridView_PWTriggers.CurrentCell.Value);
                        break;
                    
                    case 15:
                        pwnpcgen.triggers[r].month_2 = Convert.ToInt32(dataGridView_PWTriggers.CurrentCell.Value);
                        break;
                    
                    case 16:
                        pwnpcgen.triggers[r].week_day_2 = Column43.Items.IndexOf(dataGridView_PWTriggers.CurrentCell.Value) - 1;
                        break;
                    
                    case 17:
                        pwnpcgen.triggers[r].day_2 = Convert.ToInt32(dataGridView_PWTriggers.CurrentCell.Value);
                        break;
                    
                    case 18:
                        pwnpcgen.triggers[r].hour_2 = Convert.ToInt32(dataGridView_PWTriggers.CurrentCell.Value);
                        break;
                    
                    case 19:
                        pwnpcgen.triggers[r].minute_2 = Convert.ToInt32(dataGridView_PWTriggers.CurrentCell.Value);
                        break;
                    
                    case 20:
                        pwnpcgen.triggers[r].duration = Convert.ToInt32(dataGridView_PWTriggers.CurrentCell.Value);
                        break;
                }
            }
            catch
            {
                MessageBox.Show(Resources.NPCFormatErr);
            }
        }

        private void PWAddRowClick(object sender, EventArgs e)
        {
            if (pwnpcgen == null) return;

            if (contextPWDataGrid.SourceControl == dataGridView_PWCreatures)
            {
                pwnpcgen.creature_sets_count++;
                Array.Resize(ref pwnpcgen.creature_sets, pwnpcgen.creature_sets.Length + 1);
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1] = new PWCreatureSets();

                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].spawn_mode = 0;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].creature_groups_count = 0;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].spawn_x = 0;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].spawn_alt = 0;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].spawn_z = 0;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].rot_1 = 0;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].rot_2 = 0;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].rot_3 = 0;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].spread_x = 0;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].spread_alt = 0;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].spread_z = 0;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].unknown_7 = 0;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].unknown_8 = 0;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].unknown_9 = true;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].unknown_10 = true;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].unknown_11 = true;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].unknown_12 = 0;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].trigger = 0;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].unknown_14 = 0;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].unknown_15 = 0;
                pwnpcgen.creature_sets[pwnpcgen.creature_sets.Length - 1].creature_groups = new PWCreatureGroup[0];

                dataGridView_PWCreatures.Rows.Add("0", Column1.Items[0].ToString(), "0", "0", "0", "0", "0", "0", "0", "0", "0",
                    Column12.Items[0].ToString(), "0", "True", "True", "True", "0", "0", "0", "0");

                dataGridView_PWCreatures.Rows[dataGridView_PWCreatures.Rows.Count - 1].HeaderCell.Value = "";
            }

            if (contextPWDataGrid.SourceControl == dataGridView_PWCreatureGroups)
            {
                var r = dataGridView_PWCreatures.CurrentCell.RowIndex;

                pwnpcgen.creature_sets[r].creature_groups_count++;
                Array.Resize(ref pwnpcgen.creature_sets[r].creature_groups, pwnpcgen.creature_sets[r].creature_groups.Length + 1);
                pwnpcgen.creature_sets[r].creature_groups[pwnpcgen.creature_sets[r].creature_groups.Length - 1] = new PWCreatureGroup();

                pwnpcgen.creature_sets[r].creature_groups[pwnpcgen.creature_sets[r].creature_groups.Length - 1].id = 0;
                pwnpcgen.creature_sets[r].creature_groups[pwnpcgen.creature_sets[r].creature_groups.Length - 1].amount = 0;
                pwnpcgen.creature_sets[r].creature_groups[pwnpcgen.creature_sets[r].creature_groups.Length - 1].respawn = 0;
                pwnpcgen.creature_sets[r].creature_groups[pwnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_2 = 0;
                pwnpcgen.creature_sets[r].creature_groups[pwnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_3 = 0;
                pwnpcgen.creature_sets[r].creature_groups[pwnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_4 = 0;
                pwnpcgen.creature_sets[r].creature_groups[pwnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_5 = 0;
                pwnpcgen.creature_sets[r].creature_groups[pwnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_6 = 0;
                pwnpcgen.creature_sets[r].creature_groups[pwnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_7 = 0;
                pwnpcgen.creature_sets[r].creature_groups[pwnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_8 = 0;
                pwnpcgen.creature_sets[r].creature_groups[pwnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_9 = false;
                pwnpcgen.creature_sets[r].creature_groups[pwnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_10 = true;
                pwnpcgen.creature_sets[r].creature_groups[pwnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_11 = true;
                pwnpcgen.creature_sets[r].creature_groups[pwnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_12 = true;
                pwnpcgen.creature_sets[r].creature_groups[pwnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_13 = 0;
                pwnpcgen.creature_sets[r].creature_groups[pwnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_14 = 0;
                pwnpcgen.creature_sets[r].creature_groups[pwnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_15 = 0;
                pwnpcgen.creature_sets[r].creature_groups[pwnpcgen.creature_sets[r].creature_groups.Length - 1].unknown_16 = 0;

                dataGridView_PWCreatureGroups.Rows.Add("0", "0", "0", "0", "0", "0", "0", "0", "0", "0",
                    "False", "True", "True", "True", "0", "0", "0", "0");

                if (pwnpcgen.creature_sets[r].creature_groups.Length > 0)
                {
                    dataGridView_PWCreatures.Rows[r].HeaderCell.Value = "[" +
                        pwnpcgen.creature_sets[r].creature_groups[0].id.ToString(CultureInfo.InvariantCulture) + "] " +
                        PWNPCName(pwnpcgen.creature_sets[r].creature_groups[0].id);
                }
                else
                {
                    dataGridView_PWCreatures.Rows[r].HeaderCell.Value = "";
                }

                dataGridView_PWCreatures.Rows[r].Cells[0].Value = (dataGridView_PWCreatureGroups.Rows.Count).ToString(CultureInfo.InvariantCulture);
            }

            if (contextPWDataGrid.SourceControl == dataGridView_PWResources)
            {
                pwnpcgen.resource_sets_count++;
                Array.Resize(ref pwnpcgen.resource_sets, pwnpcgen.resource_sets.Length + 1);
                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1] = new PWResourceSets();

                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1].spawn_x = 0;
                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1].spawn_alt = 0;
                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1].spawn_z = 0;
                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1].spread_x = 0;
                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1].spread_z = 0;
                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1].resource_groups_count = 0;
                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1].unknown_1 = true;
                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1].unknown_2 = true;
                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1].unknown_3 = true;
                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1].unknown_4 = 0;
                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1].unknown_5a = 192;
                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1].unknown_5b = 63;
                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1].unknown_5c = 0;
                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1].unknown_trigger = 0;
                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1].unknown_6 = 0;
                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1].unknown_7 = false;
                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1].unknown_8 = false;
                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1].unknown_9 = false;
                pwnpcgen.resource_sets[pwnpcgen.resource_sets.Length - 1].resource_groups = new PWResourceGroup[0];

                dataGridView_PWResources.Rows.Add("0", "0", "0", "0", "0", "0", "True", "True", "True",
                    "0", "192", "63", "0", "0", "0", "False", "False", "False");
                dataGridView_PWResources.Rows[dataGridView_PWResources.Rows.Count - 1].HeaderCell.Value = "";
            }

            if (contextPWDataGrid.SourceControl == dataGridView_PWResourceGroups)
            {
                var r = dataGridView_PWResources.CurrentCell.RowIndex;

                pwnpcgen.resource_sets[r].resource_groups_count++;
                Array.Resize(ref pwnpcgen.resource_sets[r].resource_groups, pwnpcgen.resource_sets[r].resource_groups.Length + 1);
                pwnpcgen.resource_sets[r].resource_groups[pwnpcgen.resource_sets[r].resource_groups.Length - 1] = new PWResourceGroup();

                pwnpcgen.resource_sets[r].resource_groups[pwnpcgen.resource_sets[r].resource_groups.Length - 1].unknown_1 = 0;
                pwnpcgen.resource_sets[r].resource_groups[pwnpcgen.resource_sets[r].resource_groups.Length - 1].id = 0;
                pwnpcgen.resource_sets[r].resource_groups[pwnpcgen.resource_sets[r].resource_groups.Length - 1].respawn = 0;
                pwnpcgen.resource_sets[r].resource_groups[pwnpcgen.resource_sets[r].resource_groups.Length - 1].amount = 0;
                pwnpcgen.resource_sets[r].resource_groups[pwnpcgen.resource_sets[r].resource_groups.Length - 1].unknown_2 = 0;

                dataGridView_PWResourceGroups.Rows.Add("0", "0", "0", "0", "0");
                dataGridView_PWResources.Rows[r].HeaderCell.Value = pwnpcgen.resource_sets[r].resource_groups.Length > 0 ?
                    pwnpcgen.resource_sets[r].resource_groups[0].id.ToString(CultureInfo.InvariantCulture) : "";
                dataGridView_PWResources.Rows[r].Cells[0].Value = (dataGridView_PWResourceGroups.Rows.Count).ToString(CultureInfo.InvariantCulture);
            }

            if (contextPWDataGrid.SourceControl == dataGridView_PWDynamics)
            {
                pwnpcgen.dynamics_count++;
                Array.Resize(ref pwnpcgen.dynamics, pwnpcgen.dynamics.Length + 1);
                pwnpcgen.dynamics[pwnpcgen.dynamics.Length - 1] = new PWDynamic();

                pwnpcgen.dynamics[pwnpcgen.dynamics.Length - 1].id = 0;
                pwnpcgen.dynamics[pwnpcgen.dynamics.Length - 1].spawn_x = 0;
                pwnpcgen.dynamics[pwnpcgen.dynamics.Length - 1].spawn_alt = 0;
                pwnpcgen.dynamics[pwnpcgen.dynamics.Length - 1].spawn_z = 0;
                pwnpcgen.dynamics[pwnpcgen.dynamics.Length - 1].unknown_5 = 0;
                pwnpcgen.dynamics[pwnpcgen.dynamics.Length - 1].unknown_6 = 0;
                pwnpcgen.dynamics[pwnpcgen.dynamics.Length - 1].unknown_7 = 0;
                pwnpcgen.dynamics[pwnpcgen.dynamics.Length - 1].trigger = 0;
                pwnpcgen.dynamics[pwnpcgen.dynamics.Length - 1].unknown_8 = 16;

                dataGridView_PWDynamics.Rows.Add("0", "0", "0", "0", "0", "0", "0", "0", "16");
                dataGridView_PWDynamics.Rows[dataGridView_PWDynamics.Rows.Count - 1].HeaderCell.Value =
                    (dataGridView_PWDynamics.Rows.Count - 1).ToString(CultureInfo.InvariantCulture);
            }

            if (contextPWDataGrid.SourceControl != dataGridView_PWTriggers) return;

            pwnpcgen.triggers_count++;
            Array.Resize(ref pwnpcgen.triggers, pwnpcgen.triggers.Length + 1);
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1] = new PWTrigger();

            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].unknown_1 = 0;
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].unknown_2 = 0;
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].name = new byte[128];
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].unknown_3 = false;
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].unknown_4 = 0;
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].unknown_5 = 0;
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].unknown_6 = false;
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].unknown_7 = false;
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].year_1 = -1;
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].month_1 = -1;
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].week_day_1 = -1;
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].day_1 = -1;
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].hour_1 = -1;
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].minute_1 = -1;
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].year_2 = -1;
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].month_2 = -1;
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].week_day_2 = -1;
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].day_2 = -1;
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].hour_2 = -1;
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].minute_2 = -1;
            pwnpcgen.triggers[pwnpcgen.triggers.Length - 1].duration = 0;

            dataGridView_PWTriggers.Rows.Add("0", "0", "", "False", "0", "0", "False", "False", "-1", "-1",
                Column42.Items[0].ToString(), "-1", "-1", "-1", "-1", "-1", Column43.Items[0].ToString(), "-1", "-1", "-1", "0");
            dataGridView_PWTriggers.Rows[dataGridView_PWTriggers.Rows.Count - 1].HeaderCell.Value =
                (dataGridView_PWTriggers.Rows.Count - 1).ToString(CultureInfo.InvariantCulture);
        }

        private void PWDeleteRowClick(object sender, EventArgs e)
        {
            if (pwnpcgen == null) return;

            if (contextPWDataGrid.SourceControl == dataGridView_PWCreatures)
            {
                if (pwnpcgen.creature_sets_count > 0 && dataGridView_PWCreatures.CurrentCell.RowIndex > -1)
                {
                    pwnpcgen.creature_sets_count--;

                    var tempCS = new PWCreatureSets[pwnpcgen.creature_sets_count];
                    Array.Copy(pwnpcgen.creature_sets, 0, tempCS, 0, dataGridView_PWCreatures.CurrentCell.RowIndex);
                    Array.Copy(pwnpcgen.creature_sets, dataGridView_PWCreatures.CurrentCell.RowIndex + 1, tempCS,
                        dataGridView_PWCreatures.CurrentCell.RowIndex, pwnpcgen.creature_sets_count - dataGridView_PWCreatures.CurrentCell.RowIndex);
                    pwnpcgen.creature_sets = tempCS;

                    dataGridView_PWCreatureGroups.Rows.Clear();
                    dataGridView_PWCreatures.Rows.RemoveAt(dataGridView_PWCreatures.CurrentCell.RowIndex);
                }
            }

            if (contextPWDataGrid.SourceControl == dataGridView_PWCreatureGroups)
            {
                if (dataGridView_PWCreatures.CurrentCell.RowIndex > -1)
                {
                    var r = dataGridView_PWCreatures.CurrentCell.RowIndex;

                    if (pwnpcgen.creature_sets[r].creature_groups_count > 0 && dataGridView_PWCreatureGroups.CurrentCell.RowIndex > -1)
                    {
                        pwnpcgen.creature_sets[r].creature_groups_count--;

                        var tempCG = new PWCreatureGroup[pwnpcgen.creature_sets[r].creature_groups_count];
                        Array.Copy(pwnpcgen.creature_sets[r].creature_groups, 0, tempCG, 0, dataGridView_PWCreatureGroups.CurrentCell.RowIndex);
                        Array.Copy(pwnpcgen.creature_sets[r].creature_groups, dataGridView_PWCreatureGroups.CurrentCell.RowIndex + 1, tempCG,
                            dataGridView_PWCreatureGroups.CurrentCell.RowIndex,
                            pwnpcgen.creature_sets[r].creature_groups_count - dataGridView_PWCreatureGroups.CurrentCell.RowIndex);
                        pwnpcgen.creature_sets[r].creature_groups = tempCG;

                        dataGridView_PWCreatureGroups.Rows.RemoveAt(dataGridView_PWCreatureGroups.CurrentCell.RowIndex);

                        if (pwnpcgen.creature_sets[r].creature_groups.Length > 0)
                        {
                            dataGridView_PWCreatures.Rows[r].HeaderCell.Value =
                                "[" + pwnpcgen.creature_sets[r].creature_groups[0].id.ToString(CultureInfo.InvariantCulture) + "] " +
                                PWNPCName(pwnpcgen.creature_sets[r].creature_groups[0].id);
                        }
                        else
                        {
                            dataGridView_PWCreatures.Rows[r].HeaderCell.Value = "";
                        }

                        dataGridView_PWCreatures.Rows[r].Cells[0].Value =
                            (dataGridView_PWCreatureGroups.Rows.Count).ToString(CultureInfo.InvariantCulture);
                    }
                }
            }

            if (contextPWDataGrid.SourceControl == dataGridView_PWResources)
            {
                if (pwnpcgen.resource_sets_count > 0 && dataGridView_PWResources.CurrentCell.RowIndex > -1)
                {
                    pwnpcgen.resource_sets_count--;

                    var tempRS = new PWResourceSets[pwnpcgen.resource_sets_count];
                    Array.Copy(pwnpcgen.resource_sets, 0, tempRS, 0, dataGridView_PWResources.CurrentCell.RowIndex);
                    Array.Copy(pwnpcgen.resource_sets, dataGridView_PWResources.CurrentCell.RowIndex + 1, tempRS,
                        dataGridView_PWResources.CurrentCell.RowIndex, pwnpcgen.resource_sets_count - dataGridView_PWResources.CurrentCell.RowIndex);
                    pwnpcgen.resource_sets = tempRS;

                    dataGridView_PWResourceGroups.Rows.Clear();
                    dataGridView_PWResources.Rows.RemoveAt(dataGridView_PWResources.CurrentCell.RowIndex);
                }
            }

            if (contextPWDataGrid.SourceControl == dataGridView_PWResourceGroups)
            {
                if (dataGridView_PWResources.CurrentCell.RowIndex > -1)
                {
                    var r = dataGridView_PWResources.CurrentCell.RowIndex;

                    if (pwnpcgen.resource_sets[r].resource_groups_count > 0 && dataGridView_PWResourceGroups.CurrentCell.RowIndex > -1)
                    {
                        pwnpcgen.resource_sets[r].resource_groups_count--;

                        var tempRG = new PWResourceGroup[pwnpcgen.resource_sets[r].resource_groups_count];
                        Array.Copy(pwnpcgen.resource_sets[r].resource_groups, 0, tempRG, 0, dataGridView_PWResourceGroups.CurrentCell.RowIndex);
                        Array.Copy(pwnpcgen.resource_sets[r].resource_groups, dataGridView_PWResourceGroups.CurrentCell.RowIndex + 1, tempRG,
                            dataGridView_PWResourceGroups.CurrentCell.RowIndex,
                            pwnpcgen.resource_sets[r].resource_groups_count - dataGridView_PWResourceGroups.CurrentCell.RowIndex);
                        pwnpcgen.resource_sets[r].resource_groups = tempRG;

                        dataGridView_PWResourceGroups.Rows.RemoveAt(dataGridView_PWResourceGroups.CurrentCell.RowIndex);
                        dataGridView_PWResources.Rows[r].HeaderCell.Value = pwnpcgen.resource_sets[r].resource_groups.Length > 0 ?
                            pwnpcgen.resource_sets[r].resource_groups[0].id.ToString(CultureInfo.InvariantCulture) : "";
                        dataGridView_PWResources.Rows[r].Cells[0].Value = (dataGridView_PWResourceGroups.Rows.Count).ToString(CultureInfo.InvariantCulture);
                    }
                }
            }

            if (contextPWDataGrid.SourceControl == dataGridView_PWDynamics)
            {
                if (pwnpcgen.dynamics_count > 0 && dataGridView_PWDynamics.CurrentCell.RowIndex > -1)
                {
                    pwnpcgen.dynamics_count--;

                    var tempD = new PWDynamic[pwnpcgen.dynamics_count];
                    Array.Copy(pwnpcgen.dynamics, 0, tempD, 0, dataGridView_PWDynamics.CurrentCell.RowIndex);
                    Array.Copy(pwnpcgen.dynamics, dataGridView_PWDynamics.CurrentCell.RowIndex + 1, tempD,
                        dataGridView_PWDynamics.CurrentCell.RowIndex, pwnpcgen.dynamics_count - dataGridView_PWDynamics.CurrentCell.RowIndex);
                    pwnpcgen.dynamics = tempD;

                    dataGridView_PWDynamics.Rows.RemoveAt(dataGridView_PWDynamics.CurrentCell.RowIndex);
                }
            }

            if (contextPWDataGrid.SourceControl != dataGridView_PWTriggers) return;

            if (pwnpcgen.triggers_count <= 0 || dataGridView_PWTriggers.CurrentCell.RowIndex <= -1) return;
            pwnpcgen.triggers_count--;

            var tempT = new PWTrigger[pwnpcgen.triggers_count];
            Array.Copy(pwnpcgen.triggers, 0, tempT, 0, dataGridView_PWTriggers.CurrentCell.RowIndex);
            Array.Copy(pwnpcgen.triggers, dataGridView_PWTriggers.CurrentCell.RowIndex + 1, tempT,
                       dataGridView_PWTriggers.CurrentCell.RowIndex, pwnpcgen.triggers_count - dataGridView_PWTriggers.CurrentCell.RowIndex);
            pwnpcgen.triggers = tempT;

            dataGridView_PWTriggers.Rows.RemoveAt(dataGridView_PWTriggers.CurrentCell.RowIndex);
        }

        

    }
}
